package com.cbj.db_work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cbj.db_work.entity.News;
import com.cbj.db_work.entity.PlayerNews;
import com.cbj.db_work.entity.TeamNews;
import com.cbj.db_work.entity.vo.NewsVo;
import com.cbj.db_work.mapper.NewsMapper;
import com.cbj.db_work.service.NewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cbj.db_work.service.PlayerNewsService;
import com.cbj.db_work.service.TeamNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cbj
 * @since 2021-07-03
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    private PlayerNewsService playerNewsService;
    @Autowired
    private TeamNewsService teamNewsService;
    @Override
    public boolean addNews(NewsVo vo) {
        News news = new News();
        BeanUtils.copyProperties(vo,news);
        int insert = baseMapper.insert(news);
        String newsId = news.getId();
        if(insert==0)
            return false;
        List<String> playerId = vo.getPlayerId();
        List<String> teamId = vo.getTeamId();
        for (String s : playerId) {
            PlayerNews playerNews = new PlayerNews();
            playerNews.setNewsId(newsId);
            playerNews.setPlayerId(s);
            boolean save = playerNewsService.save(playerNews);
            if(!save)
                return false;
        }
        for (String s : teamId) {
            TeamNews teamNews = new TeamNews();
            teamNews.setNewsId(newsId);
            teamNews.setTeamId(s);
            boolean save = teamNewsService.save(teamNews);
            if(!save)
                return false;
        }
        return true;
    }

    @Override
    public List<News> getNewsTopSix() {
        QueryWrapper<News> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_date");
        List<News> list = baseMapper.selectList(wrapper);
        list.subList(0,5);
        return list;
    }
}
